import { ref } from 'vue';
import { defineStore } from 'pinia';

export const useUserStore = defineStore(
  'user',
  () => {
    const isOpen = ref(true);
    // 设置isOpen
    const setIsOpen = () => {
      isOpen.value = !isOpen.value;
    };

    // 用户信息
    const user = ref({}) as any;

    // 用户权限
    const userPower = ref([]) as any;

    // 设置用户，登录后使用
    const setUser = (u:any) => {
      user.value = u;
    };

    // 设置权限，登录后使用
    const setUserPower = (u:any) => {
      userPower.value = u;
    };

    // 清空用户，清空权限退出后使用
    const delUser = () => {
      user.value = undefined;
      userPower.value = undefined;
    };

    return { user, setUser, delUser, userPower, setUserPower, isOpen, setIsOpen };
  },
  {
    persist: true, // 持久化
  },
);
